package com.stn.zjx.givingtreasure.adapter;

import android.annotation.SuppressLint;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.base.BaseActivity;
import com.stn.zjx.givingtreasure.demo.DropItem;
import com.stn.zjx.givingtreasure.event.DropSelectEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class DropItemListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

   public List<DropItem> dataBeanList=new ArrayList<>();
   private LayoutInflater inflater;
   public BaseActivity context;
   public int pos=0;
   public int flag=10;
   public DropItemListAdapter(BaseActivity context,int flag) {
       inflater = LayoutInflater.from(context);
      this.context=context;
      this.flag=flag;
   }

   @Override
   public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       RecyclerView.ViewHolder vh = new IndexHolder(inflater.inflate(R.layout.item_drop_select, parent, false));
       return vh;
   }
   @SuppressLint("SetTextI18n")
   @Override
   public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
                   IndexHolder ih= (IndexHolder) viewHolder;
             DropItem data=dataBeanList.get(position);
          ih.name.setText(data.orderName);
          if(pos==position){
              ih.name.setTextColor(ActivityCompat.getColor(context,R.color.default_red1));
              ih.radio.setImageResource(R.drawable.language_radio_select);
          }else{
              ih.radio.setImageResource(R.drawable.language_radio_noselect);
              ih.name.setTextColor(ActivityCompat.getColor(context,R.color.gray));
          }
          ih.rootView.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                   change(position,data);
              }
          });
   }
    void change(int position,DropItem item){
           pos=position;
         DropSelectEvent event=new DropSelectEvent();
         event.flag=flag;
         event.position=position;
          event.data=new Gson().toJson(item);
        EventBus.getDefault().post(event);
         notifyDataSetChanged();
   }
   @Override
   public int getItemCount() {
       return dataBeanList.size();
   }
   public void refreshList(List<DropItem> dataBeans) {
       dataBeanList.removeAll(dataBeanList);
       dataBeanList.addAll(dataBeans);
       notifyDataSetChanged();
   }

   public void appendList(List<DropItem> dataBeans) {
       dataBeanList.addAll(dataBeans);
       notifyDataSetChanged();
   }

   public void delete(int position){
        dataBeanList.remove(position);
        notifyDataSetChanged();
   }

   public void clearList() {
       dataBeanList.removeAll(this.dataBeanList);
       notifyDataSetChanged();
   }





   public  class IndexHolder extends RecyclerView.ViewHolder{

             public View rootView;
           public TextView name;
           public ImageView radio;
        IndexHolder(@NonNull View itemView) {
           super(itemView);
           rootView=itemView;
            radio=itemView.findViewById(R.id.radio);
            name=itemView.findViewById(R.id.name);

        }
   }



}